package com.xomily.devops.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xomily.common.utils.DateUtils;
import com.xomily.common.utils.sign.Md5Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xomily.devops.mapper.DevDiscLogMapper;
import com.xomily.devops.mapper.DevDiscMapper;
import com.xomily.devops.domain.DevAlarmLog;
import com.xomily.devops.domain.DevDisc;
import com.xomily.devops.domain.DevDiscLog;
import com.xomily.devops.service.IDevDiscLogService;
import com.xomily.devops.service.IDevAlarmLogService;
import com.xomily.system.service.ISysConfigService;

/**
 * 磁盘运维日志Service业务层处理
 * 
 * @author xomily
 * @date 2021-11-29
 */
@Service
public class DevDiscLogServiceImpl implements IDevDiscLogService 
{
    @Autowired
    private DevDiscLogMapper devDiscLogMapper;
    @Autowired
    private DevDiscMapper devDiscMapper;
    @Autowired
    private IDevAlarmLogService alarmLogService;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询磁盘运维日志
     * 
     * @param id 磁盘运维日志主键
     * @return 磁盘运维日志
     */
    @Override
    public DevDiscLog selectDevDiscLogById(Long id)
    {
        return devDiscLogMapper.selectDevDiscLogById(id);
    }

    /**
     * 查询磁盘运维日志列表
     * 
     * @param devDiscLog 磁盘运维日志
     * @return 磁盘运维日志
     */
    @Override
    public List<DevDiscLog> selectDevDiscLogList(DevDiscLog devDiscLog)
    {
        return devDiscLogMapper.selectDevDiscLogList(devDiscLog);
    }

    /**
     * 查询磁盘运维日志列表
     * 
     * @param Map 条件集合
     * @return 磁盘运维日志集合
     */

    public Map<String, Object> selectDevDiscLogByGraph(Map<String, Object> params) {
		double[] data = new double[240];
		String[] xAxis = new String[240];
		List<DevDiscLog> logList = devDiscLogMapper.selectDevDiscLogByGraph(params);
		int h,m,n;
		
		for(int i=0; i<240; i++){
			h = (i/10);
			m = (i%10*6);
			xAxis[i]= String.format("%02d",h)+":"+String.format("%02d",m); 
		} 
		
		for (DevDiscLog log : logList) {
            Date createTime = log.getCreateTime();
            h = DateUtils.getHour(createTime);
            m = DateUtils.getMinute(createTime); 
            n = (h*60 + m)/6;			
			if (log.getUseRate()>data[n]) {
				data[n]= log.getUseRate();
			}
		}		
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("discGraph",data);
        ret.put("xAxis",xAxis);
		return ret;
	}
    
    /**
     * 新增磁盘运维日志
     * 
     * @param devDiscLog 磁盘运维日志
     * @return 结果
     */
    @Override
    public int insertDevDiscLog(DevDiscLog devDiscLog)
    {
        devDiscLog.setCreateTime(DateUtils.getNowDate());
        return devDiscLogMapper.insertDevDiscLog(devDiscLog);
    }

    /**
     * 新增磁盘运维日志
     * 
     * @param devDiscLog 磁盘运维日志
     * @return 结果
     */
    public int insertDevDiscLogByAgent(DevDiscLog devDiscLog)
    {
    	DevDisc discQuery = new DevDisc();
        discQuery.setIp(devDiscLog.getIp());
        discQuery.setName(devDiscLog.getName());
        DevDisc disc=devDiscMapper.selectDevDiscByIpName(discQuery);
        if ( disc!=null) {
             	disc.setSize(devDiscLog.getSize());
            	disc.setUsed(devDiscLog.getUsed());
            	disc.setUseRate(devDiscLog.getUseRate());
            	double avail = devDiscLog.getSize() - devDiscLog.getUsed(); 
            	disc.setAvail(avail);
                disc.setUpdateTime(DateUtils.getNowDate());
                devDiscLog.setDiscId(disc.getId());
            	devDiscMapper.updateDevDisc(disc);       
        }
        
	    double discUseRate = Double.parseDouble(configService.selectConfigByKey("dev.host.discUseRate"));
        if ((discUseRate>0) && (devDiscLog.getUseRate()>discUseRate)) {
        	DevAlarmLog alarmLog = new DevAlarmLog();
            String content = devDiscLog.getName()+" 磁盘使用率大于告警值"+ configService.selectConfigByKey("dev.host.discUseRate")+"%";
        	alarmLog.setType("DiscWarn");
            alarmLog.setIp(devDiscLog.getIp());
            alarmLog.setCreateTime(DateUtils.getNowDate());
            alarmLog.setGenerateTime(devDiscLog.getGenerateTime());
            alarmLog.setContent(content);
            alarmLog.setCode( Md5Utils.hash(alarmLog.getType()+alarmLog.getContent()));
            alarmLogService.insertDevAlarmLog(alarmLog);
        }
        
        
        devDiscLog.setCreateTime(DateUtils.getNowDate());
        return devDiscLogMapper.insertDevDiscLog(devDiscLog);
    }
    /**
     * 修改磁盘运维日志
     * 
     * @param devDiscLog 磁盘运维日志
     * @return 结果
     */
    @Override
    public int updateDevDiscLog(DevDiscLog devDiscLog)
    {
        return devDiscLogMapper.updateDevDiscLog(devDiscLog);
    }

    /**
     * 批量删除磁盘运维日志
     * 
     * @param ids 需要删除的磁盘运维日志主键
     * @return 结果
     */
    @Override
    public int deleteDevDiscLogByIds(Long[] ids)
    {
        return devDiscLogMapper.deleteDevDiscLogByIds(ids);
    }

    /**
     * 删除磁盘运维日志信息
     * 
     * @param id 磁盘运维日志主键
     * @return 结果
     */
    @Override
    public int deleteDevDiscLogById(Long id)
    {
        return devDiscLogMapper.deleteDevDiscLogById(id);
    }
}
